package com.asbd.springboot.controller;

import com.asbd.springboot.common.Result;
import com.asbd.springboot.entity.User;
import com.asbd.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能：
 * 作者：ASBD
 * 日期：2024/12/7 11:56
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 新增用户信息
     *
     * @return
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        userService.insertUser(user);
        return Result.success();
    }

    /**
     * 根据id修改用户信息
     *
     * @return
     */
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 根据id删除用户信息
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }

    /**
     * 批量删除用户信息
     *
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchDeleteUser(@RequestBody List<Integer> ids) {
        userService.batchDeleteUser(ids);
        return Result.success();
    }

    /**
     * 查询全部用户信息
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAllUser() {

        List<User> userList = userService.selectAllUser();
        return Result.success(userList);
    }

    /**
     * 根据Id查询用户信息
     *
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectUserById(@PathVariable Integer id) {

        User user = userService.selectUserById(id);
        return Result.success(user);
    }




}

